package com.smarttable.ysd.adapter;

import java.util.ArrayList;

import com.hp.hpl.sparta.Text;
import com.smarttable.ysd.R;
import com.smarttable.ysd.bean.AppInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryDetailGridViewAdapter extends BaseAdapter {
  private LayoutInflater mInflater;
  private Context mContext;
  private ArrayList<AppInfo> mApps;
  public CategoryDetailGridViewAdapter(Context context,ArrayList<AppInfo> apps){
    mContext = context;
    mInflater = LayoutInflater.from( mContext );
    mApps = apps;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return mApps.size()+1;
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return mApps.get(position);
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if( convertView == null )
    {
        holder = new ViewHolder( );
        convertView = mInflater.inflate( R.layout.category_detail_grid_item , null );
        holder.img_item = (ImageView)convertView.findViewById( R.id.item_app_icon );
        holder.name_item = (TextView)convertView.findViewById(R.id.item_app_name);
        convertView.setTag( holder );
    }
    else
    {
        holder = (ViewHolder)convertView.getTag( );
    }
    
    holder.img_item.setImageBitmap(null);
    if (mApps==null || mApps.size()==position) {
       holder.img_item.setBackgroundResource(R.drawable.img_add_style);
       holder.img_item.setTag(R.id.category_detail_grid_item_type_id,"add");
       holder.name_item.setText("");
    }else {
      AppInfo info = mApps.get(position);
      holder.img_item.setImageDrawable(info.getAppIcon());
      holder.img_item.setBackgroundResource(R.drawable.app_letter_grid_item_bg);
      holder.img_item.setTag(R.id.category_detail_grid_item_type_id,"app");
      holder.img_item.setTag(R.id.category_detail_grid_item_icon_id, info);
      holder.name_item.setText(info.getAppName());
    }
    return convertView;
  }

  
  static class ViewHolder
  {
  ImageView img_item;
  TextView  name_item;
  }
}
